package com.naiterui.ehp.bs.cms.modules.wms.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naiterui.ehp.bp.bo.b2c.cms.CmsSkuBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.utils.ConfigUtil;
import com.naiterui.ehp.bs.cms.modules.wms.entity.Sku;
import com.naiterui.ehp.bs.cms.modules.wms.feign.ISkuFeignClient;
import com.naiterui.ehp.bs.cms.modules.wms.mapper.MedicineMapper;
import com.naiterui.ehp.bs.cms.modules.wms.mapper.SkuImageMapper;
import com.naiterui.ehp.bs.cms.modules.wms.mapper.SkuMapper;
import com.naiterui.ehp.bs.cms.modules.wms.service.ISkuService;
import com.naiterui.ehp.bs.cms.modules.wms.vo.MedicineVO;
import com.naiterui.ehp.bs.cms.modules.wms.vo.SkuManageVO;
import com.naiterui.ehp.bs.cms.modules.wms.vo.SkuVO;

@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements ISkuService {

    @Value("${ehp.temppath}")
    private String tempUploadPath;
    @Autowired
    private MedicineMapper medicineMapper;
    @Autowired
    private ISkuFeignClient skuFeignClient;
    @Autowired
    private SkuImageMapper skuImageMapper;

    @Override
    public SkuManageVO getSkuManage(Long productId) {
        SkuManageVO skuManageVO = new SkuManageVO();

        MedicineVO medicine = this.medicineMapper.getByProductId(productId);
        skuManageVO.setMedicine(medicine);
        List<SkuVO> list = this.baseMapper.getSkuByProductId(productId);
        list.forEach(skuVO -> {
            List<String> images = this.skuImageMapper.getBySkuId(skuVO.getId(), 1);
            skuVO.setImages(images);
        });
        skuManageVO.setList(list);
        return skuManageVO;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String fileName = UUID.randomUUID() + "." + FilenameUtils.getExtension(file.getOriginalFilename());
        String imgHost = ConfigUtil.getString("sku.images.url");
        FileUtils.copyInputStreamToFile(file.getInputStream(), new File(this.tempUploadPath + "temp/" + fileName));
        return imgHost + "temp/" + fileName;
    }

    @Override
    public void updateSku(SkuVO skuVO) throws BusinessException {
        List<String> images = skuVO.getImages();
        String imgHost = ConfigUtil.getString("sku.images.url");
        List<String> replaceImages = images.stream().map(s -> s.replace(imgHost, this.tempUploadPath)).collect(Collectors.toList());
        skuVO.setImages(replaceImages);
        CmsSkuBO cmsSku = new CmsSkuBO();
        BeanUtils.copyProperties(skuVO, cmsSku);
        this.skuFeignClient.saveOrUpdate(cmsSku);

    }
}
